import {
    LOGIN_PENDING,
    LOGIN_SUCCESS,
    LOGIN_ERROR,
    LOGOUT_SUCCESS,
    FETCH_PROFILE_PENDING,
    FETCH_PROFILE_SUCCESS,
    FETCH_PROFILE_ERROR
} from '../actions/system';

const LOCATION_CHANGE = 'LOCATION_CHANGE';

const initialState = {
    user: null,
    logined: false
};

export default function auth(state = initialState, action = {}) {
    console.log('system reducers..' + action.type);
    switch (action.type) {
        case LOGIN_SUCCESS:
            return {
                ...state,
                user: action.payload.user,
                logined: true
            };
        case LOGIN_ERROR:
            return {
                ...state,
                loggingIn: false,
                user: null,
                logined: false,
                loginErrors: action.payload.message
            };

        case LOGOUT_SUCCESS:
            return {
                ...state,
                user: null,
                logined: false
            };
        case FETCH_PROFILE_SUCCESS:
            return Object.assign({}, state, {
                user: action.payload.user
            });
        case FETCH_PROFILE_ERROR:
            return Object.assign({}, state, {
                user: null,
                logined: false,
                loggingIn: false,
                loginErrors: null
            });
        default:
            return state;
    }
}
